package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealCheckGroupDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.Setmeal;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;
    @Override
    public void add(Setmeal setmeal, List<Integer> checkGroupIds) {
        setmealDao.insertSetmeal(setmeal);

        Integer setmealId = setmeal.getId();

        for (Integer checkGroupId : checkGroupIds) {
            setmealCheckGroupDao.insertsetmealCheckGroupDao(setmealId,checkGroupId);
        }

    }

    @Override
    public PageResult pageQuery(QueryPageBeanDTO queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<Setmeal> setmealPage = setmealDao.searchSetmeals(queryPageBean.getQueryString());

        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());

    }
}
